<?php

namespace App\Http\Controllers\SystemConf;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Process;
use Illuminate\Support\Facades\Redirect;


class ProcessController extends Controller
{
    //GET /photo 索引 photo.index
    public function index(){
        $data = Process::all();

        $tree = $this->tree($data->toArray(),'name','id','pid');    //对进程类进行树状排序

        return view('process.index')->withProcesses($tree);
    }
    //GET /photo/{photo} 显示 photo.show
    public function show($id){
        return 'this is show '.$id;
    }
    //POST /photo 保存 photo.store
    public function store(Request $request){
        $Process = new Process();
        $Process->pid = $request->pid;
        $Process->name = $request->name;
        $Process->sort = $request->sort;

        if($Process->save()){
            return Redirect::to('/process')->with('message','添加成功');
        }else{
            return Redirect::back()->withInput()->withErrors('添加失败');
        }

    }
    //GET /photo/create 创建 photo.create
    public function create(){
        $data = Process::all();

        $tree = $this->tree($data->toArray(),'name','id','pid');    //对进程类进行树状排序

        return view('process.add')->withProcesses($tree);
    }
    //GET /photo/{photo}/edit 编辑 photo.edit
    public function edit($id){
        $data = Process::all()->toArray();
        $Sons = $this->getSon($data,$id);
        $tree = $this->tree($data,'name','id','pid');    //对进程类进行树状排序

        foreach($tree as $k => $v){                     //对进程进行分类,如果是子级的话则不能显示,总之就是你的父级不能是你的子级
            if(in_array($v['id'],$Sons)){
                unset($tree[$k]);
            }else{
                continue;
            }
        }

        return view('process.edit')->withOld(Process::find($id))->withProcesses($tree);
    }
    //PUT/PATCH /photo/{photo} 更新 photo.update
    public function update(Request $request,$id){

        $Process = Process::find($id);
        $Process->pid = $request->pid;
        $Process->name = $request->name;
        $Process->sort = $request->sort;

        if($Process->save()){
            return Redirect::to('/process')->with('message','修改成功');
        }else{
            return Redirect::back()->withInput()->withErrors('修改失败');
        }
    }
    //DELETE /photo/{photo} 删除 photo.destroy
    //思路是删除这条数据,他的子级的pid变成被删的这条的pid
    public function destroy($id){
        $process =  Process::findOrFail($id);
        $pid = $process['pid'];
        $res = Process::where('pid','=',$id)->update(['pid'=>$pid]);
        $process->delete();
        if($res){
            return redirect('/process')->withErrors('已删除,所属子级已提升');
        }else{
            return Redirect::back()->withInput()->withErrors('已删除');
        }

    }

    // 递归获得指定id之下的所有下极菜单
    public function getSon($data,$id){
        static $arr=array();
        foreach ($data as $v) {
            if ($v['pid']==$id) {
                $arr[] = $v['id'];
                $this->getSon($data,$v['id']);
            }
        }
        return $arr;
    }

    /**
     * 获得树状数据
     * @param $data 数据
     * @param $title 字段名
     * @param string $fieldPri 主键id
     * @param string $fieldPid 父id
     * @return array
     */
    public function tree($data, $title, $fieldPri = 'cid', $fieldPid = 'pid')
    {
        if (!is_array($data) || empty($data))
            return array();
        $arr = $this->channelList($data, 0, '', $fieldPri, $fieldPid);
        foreach ($arr as $k => $v) {
            $str = "";
            if ($v['_level'] > 2) {
                for ($i = 1; $i < $v['_level'] - 1; $i++) {
                    $str .= "│&nbsp;&nbsp;&nbsp;&nbsp;";
                }
            }
            if ($v['_level'] != 1) {
                $t = $title ? $v[$title] : "";
                if (isset($arr[$k + 1]) && $arr[$k + 1]['_level'] >= $arr[$k]['_level']) {
                    $arr[$k]['_name'] = $str . "├─ " . $v['_html'] . $t;
                } else {
                    $arr[$k]['_name'] = $str . "└─ " . $v['_html'] . $t;
                }
            } else {
                $arr[$k]['_name'] = $v[$title];
            }
        }
        //设置主键为$fieldPri
        $data = array();
        foreach ($arr as $d) {
            $data[$d[$fieldPri]] = $d;
        }
        return $data;
    }
    /**
     * 获得所有子栏目
     * @param $data 栏目数据
     * @param int $pid 操作的栏目
     * @param string $html 栏目名前字符
     * @param string $fieldPri 表主键
     * @param string $fieldPid 父id
     * @param int $level 等级
     * @return array
     */
    public function channelList($data, $pid = 0, $html = "&nbsp;", $fieldPri = 'cid', $fieldPid = 'pid', $level = 1)
    {
        $data = $this->_channelList($data, $pid, $html, $fieldPri, $fieldPid, $level);
        if (empty($data))
            return $data;
        foreach ($data as $n => $m) {
            if ($m['_level'] == 1)
                continue;
            $data[$n]['_first'] = false;
            $data[$n]['_end'] = false;
            if (!isset($data[$n - 1]) || $data[$n - 1]['_level'] != $m['_level']) {
                $data[$n]['_first'] = true;
            }
            if (isset($data[$n + 1]) && $data[$n]['_level'] > $data[$n + 1]['_level']) {
                $data[$n]['_end'] = true;
            }
        }
        //更新key为栏目主键
        $category=array();
        foreach($data as $d){
            $category[$d[$fieldPri]]=$d;
        }
        return $category;
    }
    //只供channelList方法使用
    private function _channelList($data, $pid = 0, $html = "&nbsp;", $fieldPri = 'cid', $fieldPid = 'pid', $level = 1)
    {
        if (empty($data))
            return array();
        $arr = array();
        foreach ($data as $v) {
            $id = $v[$fieldPri];
            if ($v[$fieldPid] == $pid) {
                $v['_level'] = $level;
                $v['_html'] = str_repeat($html, $level - 1);
                array_push($arr, $v);
                $tmp = $this->_channelList($data, $id, $html, $fieldPri, $fieldPid, $level + 1);
                $arr = array_merge($arr, $tmp);
            }
        }
        return $arr;
    }

}
